﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Serialization;
using System.IO;
using Sripirom.ATS.Core.Logging;
using Sripirom.ATS.Core.ExceptionHandler;

namespace Sripirom.ATS.Core.Helpers
{
    public class SerializationHelper
    {
        public static string SerializeToString(Object YourClassObject)
        {
            try
            {
                // Represents an XML document, Initializes a new instance of the XmlDocument class.
                XmlDocument xmlDoc = new XmlDocument();

                XmlSerializer xmlSerializer = new XmlSerializer(YourClassObject.GetType());

                // Creates a stream whose backing store is memory. Initializes a new instance of the MemoryStream class with an expandable capacity initialized to zero.
                using (MemoryStream xmlStream = new MemoryStream())
                {
                    /* 
                    XmlSerializer.Serialize Method (XmlWriter, Object)
                    Serializes the specified Object and writes the XML document to a file using the specified xmlwriter 
            
                    Parameters
                    xmlWriter-
            
                    Type: System.Xml.XmlWriter
 
                    The XmlWriter used to write the XML document. 
                    Type: System.Object
                    The Object to serialize. 
             
                     */

                    xmlSerializer.Serialize(xmlStream, YourClassObject);
                    xmlStream.Position = 0;

                    //Loads the XML document from the specified string.
                    xmlDoc.Load(xmlStream);

                    // Pretty XML
                    System.Xml.Linq.XDocument doc = System.Xml.Linq.XDocument.Parse(xmlDoc.InnerXml);
                    return doc.ToString();
                    //return xmlDoc.InnerXml
                }
            }
            catch (InvalidOperationException)
            {
                throw new MessageException(Errors.INVALID_REQUEST_MESSAGE);
            }
            catch (Exception)
            {
                throw;
            }
        }

        public static Object DeserializeToObject(string XMLString, Object YourClassObject)
        {
            try
            {
                XmlSerializer oXmlSerializer = new XmlSerializer(YourClassObject.GetType());

                //The StringReader will be the stream holder for the existing XML file

                YourClassObject = oXmlSerializer.Deserialize(new StringReader(XMLString));

                //initially deserialized, the data is represented by an object without a defined type

                return YourClassObject;
            }
            catch (InvalidOperationException ex)
            {
                LoggerFactory.CreateLog().LogError("ex.Message : " + ex.Message);
                throw new MessageException(Errors.INVALID_REQUEST_MESSAGE);
            }
            catch (Exception)
            {
                throw;
            }
        }

        /// <summary>
        /// For Deserializable
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="xmlText"></param>
        /// <returns></returns>
        public static T Deserialize<T>(string xmlText)
        {
            try
            {
                var stringReader = new System.IO.StringReader(xmlText);
                var serializer = new XmlSerializer(typeof(T));
                return (T)serializer.Deserialize(stringReader);
            }
            catch
            {
                throw;
            }
        }
    }
}
